package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.PurchaseMapper;
import com.hqyj.dao.UserMapper;
import com.hqyj.pojo.Purchase;
import com.hqyj.pojo.Rows;
import com.hqyj.dao.RowsMapper;
import com.hqyj.pojo.User;
import com.hqyj.service.IRowsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 原料表 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-21
 */
@Service
public class RowsServiceImpl implements IRowsService {

    //注入RowsMapper
    @Autowired(required = false)
    RowsMapper rowsMapper;

    //注入purchaseMapper
    @Autowired(required = false)
    PurchaseMapper purchaseMapper;

    //注入UserMapper
    @Autowired(required = false)
    UserMapper userMapper;

    //加载分页
    @Override
    public HashMap<String, Object> select(Rows rows) {
        //创建HashMap对象
        HashMap<String,Object> map = new HashMap<>();
        //创建分页对象
        Page<Rows> page = new Page<>(rows.getPage(),rows.getRow());

        //定义条件构造器
        QueryWrapper<Rows> queryWrapper = new QueryWrapper<>();
        //查询数据库
        Page<Rows> rowsPageList = rowsMapper.selectPage(page, queryWrapper);
        //定义当前页集合
        map.put("list",rowsPageList.getRecords());
        //总页数
        map.put("totalPage",rowsPageList.getPages());
        //总条数
        map.put("total",rowsPageList.getTotal());
        //当前页
        map.put("curPage",rows.getPage());
        //如果当前页为1，则上一页为也为1
        if (rowsPageList.getCurrent() == 1){
            //上一页
            map.put("prePage",1);
        }else {
            map.put("prePage",rows.getPage()-1);
        }
        //如果当前页为最后一页，则下一页为也为最大页
        if (rows.getPage() == rowsPageList.getPages()){
            //下一页
            map.put("nextPage",rowsPageList.getPages());
        }else {
            map.put("nextPage",rows.getPage()+1);
        }
        //显示每页条数
        map.put("row",rows.getRow());
        return map;
    }

    //新增
    @Override
    public HashMap<String, Object> rowsAdd(Rows rows) {
        HashMap<String,Object> map = new HashMap<>();
        int num = rowsMapper.insert(rows);
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    //删除
    @Override
    public HashMap<String, Object> del(Rows rows) {
        HashMap<String,Object> map = new HashMap<>();
        int num = rowsMapper.deleteById(rows.getRowsId());
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    //修改
    @Override
    public HashMap<String, Object> update(Rows rows) {
        HashMap<String,Object> map = new HashMap<>();
        int num = rowsMapper.updateById(rows);
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    //购买
    @Override
    public HashMap<String, Object> purchaseOne(Rows rows,HttpSession session) {
    HashMap<String,Object> map = new HashMap<>();
        //创建Purchase对象
        Purchase purchase = new Purchase();
        //属性赋值
             //设置原料ID
        purchase.setRowsId(rows.getRowsId());
            //设置原料名
        purchase.setRowsName(rows.getRowsName());
            //设置购买数量
        purchase.setPurchaseNum(rows.getPurchaseNum());
            //设置采购人ID
        User user = (User) session.getAttribute("USER");
        List<User> users = userMapper.selectByName(user.getSysName());
        Integer sysId = users.get(0).getSysId();
        purchase.setSysId(sysId);
        //设置采购时间
        String nowDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        purchase.setPurchaseTime(nowDate);
        //设置状态
        purchase.setPurchaseState("未入库");
        System.out.println(purchase);
        int num = purchaseMapper.insert(purchase);
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

}
